import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:todo_app/models/todo_model.dart';
import 'package:todo_app/pages/providers/todo_list/todo_list_provider.dart';

class TodoItem extends ConsumerWidget {
  final Todo todo;
  const TodoItem(this.todo, {super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return ListTile(
      onTap: () {
        showDialog(
          context: context,
          builder: (context) {
            return ConfirmEditingDialog(todo);
          },
        );
      },
      leading: Checkbox(
        value: todo.completed,
        onChanged: (check) {
          ref.read(todoListProvider.notifier).toggleTodo(todo.id);
        },
      ),
      title: Text(todo.desc),
      trailing: IconButton(
        onPressed: () async {
          bool isRemove = await showDialog(
            context: context,
            barrierDismissible: false,
            builder: (context) {
              return AlertDialog(
                title: Text("确认删除？"),
                content: Text("你真的想删除吗"),
                actions: [
                  TextButton(
                    onPressed: () {
                      Navigator.pop(context, false);
                    },
                    child: Text("否"),
                  ),
                  TextButton(
                    onPressed: () {
                      Navigator.pop(context, true);
                    },
                    child: Text("是"),
                  ),
                ],
              );
            },
          );
          if (isRemove) {
            ref.read(todoListProvider.notifier).removeTodo(todo.id);
          }
        },
        icon: Icon(Icons.delete),
      ),
    );
  }
}

class ConfirmEditingDialog extends ConsumerStatefulWidget {
  final Todo todo;
  const ConfirmEditingDialog(this.todo, {super.key});

  @override
  ConsumerState<ConsumerStatefulWidget> createState() =>
      _ConfirmEditingDialogState();
}

class _ConfirmEditingDialogState extends ConsumerState<ConfirmEditingDialog> {
  late final TextEditingController textEditingController;
  bool error = false;
  @override
  void initState() {
    textEditingController = TextEditingController(text: widget.todo.desc);
    super.initState();
  }

  @override
  void dispose() {
    textEditingController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text("编辑 Todo"),
      content: TextField(
        controller: textEditingController,
        autofocus: true,
        decoration: InputDecoration(errorText: error ? "值不能为空" : null),
      ),
      actions: [
        TextButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: Text("取消"),
        ),
        TextButton(
          onPressed: () {
            error = textEditingController.text.isEmpty ? true : false;
            if (error) {
              setState(() {});
            } else {
              ref
                  .read(todoListProvider.notifier)
                  .editTodo(widget.todo.id, textEditingController.text);
              Navigator.pop(context);
            }
          },
          child: Text("更改"),
        ),
      ],
    );
  }
}
